package designPattern.singletonPattern;

import java.io.*;

/**
 * Test
 *
 * @author nanxin.chen
 * @date 2015/11/3
 */
public class Test{

    public static void main(String[] args) throws Exception {
//        Test.testLayzSingleton();
        Test.testSerSingleton();
    }

    public static void testLayzSingleton() {
        Thread[] ts1 = new Thread[10];
        Thread[] ts2 = new Thread[10];
        for (int i = 0; i < 10; i++) {
            ts1[i] = new Thread(new TestAssist1());
        }
        for (int i = 0; i < 10; i++) {
            ts2[i] = new Thread(new TestAssist2());
        }
        for (Thread t:ts1) {
            t.start();
        }
        for (Thread t : ts2) {
            t.start();
        }
    }

    public static void testSerSingleton() throws Exception {
        SerSingleton s = SerSingleton.getInstance();
        String dir = System.getProperty("user.dir");
        File f = new File(dir, "test.txt");
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream os = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(s);
        oos.flush();
        oos.close();

        FileInputStream is = new FileInputStream(f);
        ObjectInputStream iis = new ObjectInputStream(is);
        SerSingleton s2 = (SerSingleton) iis.readObject();

        System.out.println(s == s2);
    }

}
